var listOfChords = new Array("A","B","C","D","E","F","G");
var previousChords = new Array();
var fortsett = false;

$(document).ready(function() {

    //Load songs
    soundManager.url = '';
    soundManager.debugMode = false;
    soundManager.onload = function() {
        aChord = soundManager.createSound({
            id: 'aChord',
            url: 'sound/aChord.mp3'
        });
        bChord = soundManager.createSound({
            id: 'bChord',
            url: 'sound/bChord.mp3'
        });
        cChord = soundManager.createSound({
            id: 'cChord',
            url: 'sound/cChord.mp3'
        });
        dChord = soundManager.createSound({
            id: 'dChord',
            url: 'sound/dChord.mp3'
        });
        eChord = soundManager.createSound({
            id: 'eChord',
            url: 'sound/eChord.mp3'
        });
        fChord = soundManager.createSound({
            id: 'fChord',
            url: 'sound/fChord.mp3'
        });
        gChord = soundManager.createSound({
            id: 'gChord',
            url: 'sound/gChord.mp3'
        });

        aMinor = soundManager.createSound({
            id: 'aMinor',
            url: 'sound/aMinor.mp3'
        });
        bMinor = soundManager.createSound({
            id: 'bMinor',
            url: 'sound/bMinor.mp3'
        });
        cMinor = soundManager.createSound({
            id: 'cMinor',
            url: 'sound/cMinor.mp3'
        });
        dMinor = soundManager.createSound({
            id: 'dMinor',
            url: 'sound/dMinor.mp3'
        });
        eMinor = soundManager.createSound({
            id: 'eMinor',
            url: 'sound/eMinor.mp3'
        });
        fMinor = soundManager.createSound({
            id: 'fMinor',
            url: 'sound/fMinor.mp3'
        });
        gMinor = soundManager.createSound({
            id: 'gMinor',
            url: 'sound/gMinor.mp3'
        });

        listOfMajorChords = new Array(aChord, bChord, cChord, dChord, eChord, fChord, gChord);
        listOfMinorChords = new Array(aMinor, bMinor, cMinor, dMinor, eMinor, fMinor, gMinor);
    }

    createButtons();

});

function createButtons() {
    for(i=0;i<14;i++) {
        $('#buttons').append('<input type="button" id="buttonButtons" value="'+ intToAlphabet(i) + '" onclick="playChord('+i+')" />');
        if(i==6) {
            $('#buttons').append('<br />');
        }
    }
}

function playChord(numb) {
    $('#chordImg').html('<img src=img/' +getImage(numb)+' width="120px" />');
    if(Math.floor(numb/7)==0) {
        listOfMajorChords[numb].play();
    }
    else {
        listOfMinorChords[numb%7].play();
    }
    insertChord(numb);
    updatePreviousChords();
}

function insertChord(numb) {
    previousChords.push(intToAlphabet(numb));

    if(previousChords.length > 10) {
        previousChords.shift();
    }
}

function updatePreviousChords() {
    $('#previous-chord').html('<ul>');
    for(i = 0 ; i < previousChords.length; i++) {
        $('#previous-chord').prepend('<li>' + previousChords[i] + '</li>');
    }
    $('#previous-chord').append('</ul>');
}



function intToAlphabet(number) {
    switch(number) {
        case 0:
            return "A";
        case 1:
            return "B";
        case 2:
            return "C";
        case 3:
            return "D";
        case 4:
            return "E";
        case 5:
            return "F";
        case 6:
            return "G";
        case 7:
            return "Am";
        case 8:
            return "Bm";
        case 9:
            return "Cm";
        case 10:
            return "Dm";
        case 11:
            return "Em";
        case 12:
            return "Fm";
        case 13:
            return "Gm";
            break;

            return nil;
    }
}

function chordToNumber(string) {
    switch(string) {
        case "a":
            return 0;
        case "b":
            return 1;
        case "c":
            return 2;
        case "d":
            return 3;
        case "e":
            return 4;
        case "f":
            return 5;
        case "g":
            return 6;
        case "am":
            return 7;
        case "bm":
            return 8;
        case "cm":
            return 9;
        case "dm":
            return 10;
        case "em":
            return 11;
        case "fm":
            return 12;
        case "gm":
            return 13;
            break;
    }
    return -1;
}

function getImage(number) {
    switch(number) {
        case 0:
            return "a.gif";
        case 1:
            return "b.jpg";
        case 2:
            return "c.gif";
        case 3:
            return "d.gif";
        case 4:
            return "e.gif";
        case 5:
            return "f.gif";
        case 6:
            return "g.gif";
        case 7:
            return "am.gif";
        case 8:
            return "bm.gif";
        case 9:
            return "cm.gif";
        case 10:
            return "dm.gif";
        case 11:
            return "em.gif";
        case 12:
            return "fm.gif";
        case 13:
            return "gm.gif";
            break;

            return nil;
    }
}

function playFun() {
    var input = $('#textarea').val();
    var inputInPieces = input.split(' ');
    var listOfCommands = new Array();
    for(i=0;i<inputInPieces.length;i++) {
        if(parseInt(inputInPieces[i])) {
            listOfCommands.push(inputInPieces[i]);
        } else {
            if( chordToNumber( inputInPieces[i].toLowerCase() ) == -1) {
                $('#textarea').val('Input error. Please check your input.');
                break;
            }
            else {
            }
            listOfCommands.push(inputInPieces[i].toLowerCase());
        }
    }
    processCommands(listOfCommands.reverse());
}

function processCommands(listOfCommands) {

	if(listOfCommands.length == 0)
		return;

	var command = listOfCommands.pop();

	if(parseInt(command)) {
		setTimeout(function(){processCommands(listOfCommands);},command*100);
	}
	else {
        var chordNumber = chordToNumber(command);
        playChord(chordNumber);
        processCommands(listOfCommands);
	}

}